package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dk.model.CraftConstitute;
import com.dk.mapper.CraftConstituteMapper;
import com.dk.service.CraftConstituteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.vo.CraftConstituteVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-20
 */
@Service
public class CraftConstituteServiceImpl extends ServiceImpl<CraftConstituteMapper, CraftConstitute> implements CraftConstituteService {
    @Resource
    private CraftConstituteMapper craftConstituteMapper;

    @Override
    public List<CraftConstituteVO> getInfoList(String craftRouteId) {
        LambdaQueryWrapper<CraftConstitute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CraftConstitute::getCraftRouteId, craftRouteId);
        List<CraftConstituteVO> collect = craftConstituteMapper.selectList(wrapper).stream().map(craftConstitute -> {
            CraftConstituteVO vo = new CraftConstituteVO();
            BeanUtils.copyProperties(craftConstitute, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void insertInfoCraftConstitute(CraftConstituteVO craftConstituteVO) {
        CraftConstitute craftConstitute = new CraftConstitute();
        BeanUtils.copyProperties(craftConstituteVO,craftConstitute);
        craftConstituteMapper.insert(craftConstitute);
    }

    @Override
    public CraftConstituteVO getInfoCraftConstituteById(Integer id) {
        CraftConstitute craftConstitute = craftConstituteMapper.selectById(id);
        CraftConstituteVO craftConstituteVO = new CraftConstituteVO();
        BeanUtils.copyProperties(craftConstitute,craftConstituteVO);
        return craftConstituteVO;
    }

    @Override
    public void updateInfoCraftConstituteById(CraftConstituteVO craftConstituteVO) {
        CraftConstitute craftConstitute = new CraftConstitute();
        BeanUtils.copyProperties(craftConstituteVO,craftConstitute);
        craftConstituteMapper.updateById(craftConstitute);
    }

    @Override
    public void deleteInfoCraftConstituteById(Integer id) {
        craftConstituteMapper.deleteById(id);
    }

    @Override
    public void deleteInfoCraftConstituteByCraftRouteId(String craftRouteId) {
        LambdaQueryWrapper<CraftConstitute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CraftConstitute::getCraftRouteId,craftRouteId);
        craftConstituteMapper.delete(wrapper);
    }

    @Override
    public List<CraftConstitute>  getInfoCraftConstituteByRouteId(String craftRouteId) {
        LambdaQueryWrapper<CraftConstitute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CraftConstitute::getCraftRouteId,craftRouteId);
        wrapper.orderByAsc(CraftConstitute::getConstituteSequence);
        List<CraftConstitute> craftConstitutes = craftConstituteMapper.selectList(wrapper);
        return craftConstitutes;
    }
}
